package com.example.newsapp.ui.screens.otherScreens

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.database.databaseModel.NewsDatabaseModel
import com.example.newsapp.data.list.resultTitleList
import com.example.newsapp.data.`object`.PageNavigation
import com.example.newsapp.data.`object`.StateSave
import com.example.newsapp.ui.components.otherComponent.searchResultComponent.ResultList
import com.example.newsapp.ui.components.otherComponent.searchResultComponent.ResultTopBar
import com.google.gson.Gson
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

/**
 * 搜索结果页面
 *
 * @param title 新闻标题
 */
@SuppressLint("CoroutineCreationDuringComposition")
@Composable
fun SearchResultScreen(
    title: String?,
    modifier: Modifier = Modifier
) {
    val focusRequester = remember { FocusRequester() }
    val context = LocalContext.current
    val databaseModel = NewsDatabaseModel(context)
    val scope = rememberCoroutineScope()
    StateSave.gesturesEnable = false
    val navController = LocalNavController.current
    val keyboardController = LocalSoftwareKeyboardController.current
    val gson = Gson()

    // 根据title模糊查询数据库
    runBlocking {
        resultTitleList.clear()
        resultTitleList.addAll(databaseModel.dateSelect.selectByTitle("%${title}%"))
    }
    LazyColumn(
        modifier = Modifier.fillMaxSize()
            .focusRequester(focusRequester)
            .clickable(
                interactionSource = null,
                indication = null
            ) {
                focusRequester.freeFocus()
                keyboardController?.hide()
            }
            .pointerInput(Unit) {
                focusRequester.freeFocus()
                keyboardController?.hide()
            }
    ) {
        // 顶部搜索栏
        item {
            ResultTopBar(focusRequester)
        }
        // 搜索后的结果
        itemsIndexed(resultTitleList) { index, item ->
            ResultList(
                item = item,
                context = context,
                gson = gson,
                navController = navController,
                focusRequester = focusRequester
            )
        }
    }
}